package com.sc.nft.dao;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.SysMember;
import com.sc.nft.mapper.SysMemberMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.Date;

/**
 * 系统会员表(pro_sys_member)数据DAO
 *
 * @author zhangxiangyu
 * @since 2022-02-02 17:11:20
 * @description 
 */
@Slf4j
@Repository
public class SysMemberDao extends ServiceImpl<SysMemberMapper, SysMember> {

    public SysMember login(String account){
        LambdaQueryWrapper<SysMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysMember::getUserAccount,account);
        wrapper.eq(SysMember::getIsDelete,Boolean.FALSE);
        return getOne(wrapper);
    }
    public SysMember getByAccount(String account){
        LambdaQueryWrapper<SysMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysMember::getUserAccount,account);
        wrapper.eq(SysMember::getIsDelete,Boolean.FALSE);
        return getOne(wrapper);
    }

    public Page<SysMember> query(String account, Long roleId, String userName, Integer userSex, Date startBirthday, Date endBirthday, String phone, int pageNo, int pageSize){
        LambdaQueryWrapper<SysMember> wrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(account)){
            wrapper.eq(SysMember::getUserAccount,account);
        }
        if(StrUtil.isNotBlank(userName)){
            wrapper.eq(SysMember::getUserName,userName);
        }
        if(roleId!=null){
            wrapper.eq(SysMember::getRoleId,roleId);
        }
        if(userSex!=null){
            wrapper.eq(SysMember::getUserSex,userSex);
        }
        if(startBirthday!=null){
            wrapper.gt(SysMember::getUserBirthday,startBirthday);
        }
        if(endBirthday!=null){
            wrapper.lt(SysMember::getUserBirthday,endBirthday);
        }
        if(StrUtil.isNotBlank(phone)){
            wrapper.eq(SysMember::getPhone,phone);
        }
        return page(new Page<>(pageNo,pageSize),wrapper);
    }

    public SysMember selectByRoleId(Long id) {
        LambdaQueryWrapper<SysMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SysMember::getRoleId,id );
        return getOne(wrapper);
    }
}